package com.nanyang.tms.bootstrap;

import java.util.ArrayList;
import java.util.List;

import org.mybatis.spring.annotation.MapperScan;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.MatchOperation;
import org.springframework.data.mongodb.core.aggregation.ProjectionOperation;
import org.springframework.data.mongodb.core.aggregation.UnwindOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;

import com.nanyang.tms.domain.dataimport.DataImportDO;
import com.nanyang.tms.domain.dataimport.DataImportLineVO;
import com.nanyang.tms.domain.dataimport.DataImportVO;
import com.nanyang.tms.mongo.DataImportDao;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;

@SpringBootApplication
@EnableDiscoveryClient
@ComponentScan(basePackages = { "com.nanyang.tms", "com.nanyang.common" })
@MapperScan(basePackages = { "com.nanyang.tms.dao" })
@EnableMongoRepositories(basePackages = {"com.nanyang.tms.mongo"})
public class TmsServiceApplication implements CommandLineRunner{
//    @Autowired
//	private AManager aManager;
//    @Autowired
//     private BManager bManager;
	@Autowired
	DataSourceTransactionManager dataSourceTransactionManager;
	@Autowired
	TransactionDefinition transactionDefinition;
	@Autowired
	private RedissonClient redissonClient;
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private DataImportDao dataImportDao;
    @Autowired
    private MongoTemplate mongoTemplate;
        public static void main(String[] args) {
            SpringApplication.run(TmsServiceApplication.class, args);
        }

		@Override
		public void run(String... args) throws Exception {
			  
		 //   System.out.println(mongoTemplate.aggregate(aggregation, DataImportDO.class,  DataImportLineVO.class).getMappedResults().size());
//			DataImportDomain<ForecastInfo> dataImportDomain = new DataImportDomain<ForecastInfo>();
//			dataImportDomain.setId(new ObjectId("63c6cf82ff5f011e89f77fa3").toString());
//			ForecastInfo info=new ForecastInfo();
//			info.setCustomerNo("000005");
//			dataImportDomain.setData(info);
//			System.out.println(forecastImportDao.findForecastInfo(dataImportDomain).toString());
//			List<ForecastInfo> list=new ArrayList<ForecastInfo>();
//			ForecastInfo info1=new ForecastInfo();
//			info1.setSaleProductCode("3223232323");
//			info1.setCargoTypeCode("baoguo");
//			info1.setCargoTypeName("包裹");
//			info1.setCustomerNo("000001");
//			ForecastInfo info2=new ForecastInfo();
//			info2.setSaleProductCode("3223232323");
//			info2.setCargoTypeCode("baoguo");
//			info2.setCargoTypeName("包裹");
//			info2.setCustomerNo("000001");
//			list.add(info1);
//			list.add(info2);
//			
//			DataImportDomain<List<ForecastInfo>> dataImportDomain=	new DataImportDomain<List<ForecastInfo>>();
//			dataImportDomain.setId(new ObjectId("63c6cf82ff5f011e89f77fa3").toString());
//			dataImportDomain.setData(list);
//			dataImportDao.addToSet(dataImportDomain);
//			for(String str:applicationContext.getBeanDefinitionNames()){
//				System.out.println(str);
//			}
//			System.out.println("");
			//            TransactionStatus status =dataSourceTransactionManager.getTransaction(transactionDefinition);
//			aManager.saveA(status);
//			bManager.saveB(status);
//			 RSemaphore semaphore = redissonClient.getSemaphore("T22112417044936320");
//			 semaphore.trySetPermits(19);
//			 RSemaphore semaphore1 = redissonClient.getSemaphore("T22112417044936320");
//
//			 boolean tryAcquire = semaphore1.tryAcquire();
//				 System.out.println(tryAcquire);
		}

}
